<template>
  <van-field
    v-if="h5"
    v-show="designer || (item.options && !item.options.hidden)"
    v-model="fieldModel"
    type="number"
    :formatter="formatter"
    :name="item.options && item.options.name"
    :label="item.options && item.options.label"
    :readonly="isDetail || designer || (item.options && item.options.readonly)"
    :placeholder="isDetail ? '' : item.options && item.options.placeholder"
    :required="item.options && !isDetail && item.options.required"
    :rules="isDetail ? [] : rules"
    :border="false"
    :label-width="item.options?.labelPosition === 'top' ? '100%' : undefined"
    :label-align="(item.options && item.options.labelPosition) || undefined"
    :input-align="(item.options && item.options.inputAlign) || undefined"
    autocomplete="off"
    @blur="handleBlurEvent(fieldModel)"
  />
  <widget-form-item
    v-else
    v-show="designer || (item.options && !item.options.hidden)"
    :designer="designer"
    :widget-list="widgetList"
    :item="item"
    :rules="isDetail ? [] : rules"
    :index="index"
    :show-message="!isDetail"
  >
    <el-input
      v-if="!isDetail"
      v-model="fieldModel"
      type="text"
      :readonly="designer || (item.options && item.options.readonly)"
      :placeholder="item.options && item.options.placeholder"
      :min="item.options && item.options.min"
      :max="item.options && item.options.max"
      @input="handleInput"
      @blur="handleBlurEvent(fieldModel)"
    />
    <div v-else>
      {{ fieldModel }}
    </div>
  </widget-form-item>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import props from './props'

export default {
  name: 'MAmount',
  props,
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const fieldModel = computed({
      get() {
        return props.modelValue
      },
      set(val) {
        if (!props.designer) {
          emit('update:modelValue', val)
        }
      }
    })

    /** 组件权限 */
    const isDetail = computed(() => {
      if (props.detail) {
        return true
      }
      return props.item.permission > 0
    })

    const rules = reactive([
      {
        required: !props.item.options.hidden && !props.item.options.readonly && props.item.options.required,
        message: props.h5 ? '' : props.item.options.requiredMessage,
        trigger: 'change'
      }
    ])

    /** 限制输入大小范围 */
    const handleBlurEvent = (value) => {
      const limitMin = props.item.options.min || 0
      const limitMax = props.item.options.max || 999999999999999
      if (value > limitMax) {
        emit('update:modelValue', limitMax)
      }
      if (value < limitMin) {
        emit('update:modelValue', limitMin)
      }

      // 省略末尾的0
      if (props.item.options.omitEndZero && String(value).indexOf('.') !== -1) {
        const filtered = String(value).replace(/\.?0*$/, '')
        if (filtered !== String(value)) {
          emit('update:modelValue', filtered)
        }
      }
    }

    // 过滤输入的数字
    const formatter = (value) => {
      let filtered = value
        .replace(/[^\d.]/g, '') // 只允许数字和小数点
        .replace(/^0+(\d)/, '$1') // 禁止0开头
        .replace(/(\..*)\./g, '$1') // 禁止多个小数点

      // 保留小数位数处理
      if (props.item.options.decimalPlaces === 2) {
        filtered = filtered.replace(/(\.\d{2})\d+/g, '$1')
      } else {
        filtered = filtered.replace(/(\.\d)\d+/g, '$1')
      }
      return filtered
    }

    const handleInput = (value) => {
      let filtered = value
        .replace(/[^\d.]/g, '') // 只允许数字和小数点
        .replace(/^0+(\d)/, '$1') // 禁止0开头
        .replace(/(\..*)\./g, '$1') // 禁止多个小数点

      // 保留小数位数处理
      if (props.item.options.decimalPlaces === 2) {
        filtered = filtered.replace(/(\.\d{2})\d+/g, '$1')
      } else {
        filtered = filtered.replace(/(\.\d)\d+/g, '$1')
      }

      if (filtered !== value) {
        emit('update:modelValue', filtered)
      }
    }

    return {
      fieldModel,
      isDetail,
      rules,
      formatter,
      handleInput,
      handleBlurEvent
    }
  }
}
</script>
